package joey.project1.view

import java.awt.Dimension
import java.awt.Toolkit
import java.awt.Window

import javax.swing.JFrame
import javax.swing.SwingUtilities

class ViewUtil {
	public static invokeLater(Closure work){
		Runnable doWorkRunnable = new Runnable() {
					public void run() {
						work()
					}
				}
		SwingUtilities.invokeLater(doWorkRunnable)
	}

	public static centerWindow(Window frame,boolean top = false){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()//得到目前螢幕的大小
		Dimension frameSize = frame.getSize()

		// Set the height of the window
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height
		}

		// Set the width of the window
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width
		}

		// 圖型放在正中央
		int centerwidth = (screenSize.width - frameSize.width) / 2
		int centerheight = (screenSize.height - frameSize.height) / 2
		frame.setLocation(centerwidth, top?0:centerheight)
	}
}
